import { defineConfig, ConfigEnv, loadEnv } from "vite";
import { resolve } from "path";
import uni from "@dcloudio/vite-plugin-uni";
// https://vitejs.dev/config/
const pathResolve = (dir: string) => {
  return resolve(__dirname, ".", dir);
};
const alias: Record<string, string> = {
  "/static": pathResolve("./src/static"),
};
const viteConfig = defineConfig(async (mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd(), "");
  console.log(env.VITE_BASE_URL);
  const UnoCss = await import("unocss/vite").then((i) => i.default);
  return {
    plugins: [uni(), UnoCss()],
    root: process.cwd(),
    resolve: {
      alias,
    },
    build: {
      rollupOptions: {
        input: {
          "public/amap/mapWx": "./src/public/amap/mapWx.js",
          "public/amap/qqmap-wx-jssdk": "./src/public/amap/qqmap-wx-jssdk.js",
          "public/amap/qqmap-wx-jssdk.min":
            "./src/public/amap/qqmap-wx-jssdk.min.js",
        },
      },
    },
    extensions: [
      ".mjs",
      ".js",
      ".ts",
      ".jsx",
      ".tsx",
      ".json",
      ".min.js",
      ".jsx",
    ],
    base: mode.mode == "development" ? "/" : "/h5/",
    server: {
      host: true,
      port: 3366,
      cors: true,
      hmr: true,
      open: true,
      proxy: {
        "/api": {
          target: 'http://localhost:3000',
          ws: true,
          changeOrigin: true,
          //如果是https接口，需要配置这个参数
          secure: true,
          // pathRewrite: {
          //   "^/api": "", //重写地址
          // },
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
  };
});
export default viteConfig;
